<template>
  <div class="warp_auto template-select-diaolog">
    <div class="warp_top">
      <div class="jiansuo-box">
        <span>模板类型：</span>
        <el-select
          v-model="templateType"
          placeholder="请选择类型"
          clearable
          class="input"
        >
          <el-option
            v-for="item in arrTypelist"
            :key="item.id"
            :label="item.dict_template_name"
            :value="item.id"
          />
        </el-select>
      </div>
      <div class="jiansuo-box">
        <span>模板名称： </span>
        <el-input
          v-model="templateName"
          class="input"
          placeholder="请输入模板名称"
        />
      </div>
      <div class="jiansuo-box">
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn_"
          @click="btn"
        >检索
        </el-button>
      </div>
      <div class="jiansuo-box">
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn_"
          @click="selectPrescription"
        >非药嘱
        </el-button>
      </div>
    </div>

    <!--模板（表格）-->
    <div>
      <el-table
        :data="listArr_"
        :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
        style="width: 100%"
        height="500"
        stripe
      >
        <el-table-column
          type="index"
          width="60"
          label="序号"
          align="center"
        />
        <el-table-column label="类型" min-width="15%" align="center">
          <template slot-scope="scope">
             <span style="margin-left: 10px" v-for="typeItem in arrTypeAllList"
                   v-if="typeItem.id == scope.row.template_id">{{
                 typeItem.dict_template_name
               }}</span>
          </template>
        </el-table-column>
        <el-table-column label="模板名称" min-width="15%" align="center">
          <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                  scope.row.template_item_name
                }}</span>
          </template>
        </el-table-column>
        <el-table-column label="模板内容" min-width="35%" align="center">
          <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                  scope.row.content
                }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="15%">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin: 5px 0"
              @click="handleEdit(scope.row)"
            >选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align: right;margin-top: 10px"
        :current-page="start"
        :page-size="PageSize"
        :pager-count="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 非药嘱 -->
    <el-dialog
      title="选择非药嘱"
      :visible.sync="noPrescriptionDialogVisible"
      :close-on-click-modal="false"
    >
      <!--模板（表格）-->
      <div>
        <el-table
          :data="lists"
          :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
          style="width: 100%"
          height="500"
          stripe
        >
          <el-table-column
            type="index"
            width="60"
            label="序号"
            align="center"
          />
          <el-table-column label="医嘱内容" min-width="35%" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                  scope.row.orderName
                }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderStatus"
            label="状态"
            align="center"
          >
            <template slot-scope="scope">
              {{
                scope.row.orderStatus == "0"
                  ? "待审"
                  : scope.row.orderStatus == "1"
                    ? "审核"
                    : scope.row.orderStatus == "2"
                      ? "执行"
                      : scope.row.orderStatus == "3"
                        ? "取消"
                        : "停止"
              }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="15%">
            <template slot-scope="scope">
              <el-button
                size="mini"
                style="margin: 5px 0"
                @click="handleEdit2(scope.row)"
              >选择
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="text-align: right;margin-top: 10px"
          :current-page="start2"
          :page-size="PageSize2"
          :pager-count="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount2"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getListlei, getUser, medOrderExecQueryList, queryList} from '@/api/table'

export default {
  // components: {noPrescriptionTemplate},
  props: {
    rowIndex: {
      type: Number,
      required: true
    },
    patientId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      start2: 1,
      PageSize2: 10,
      totalCount2: 0,
      lists: [],
      orderTime: '',
      noPrescriptionDialogVisible: false, //非药嘱对话框
      dialogUpdatalei: false, // 开关-类型
      dialogUpdata: false, // 开关-模板
      leiswitchId: '',
      leiswitchStatus: '',
      switchId: '',
      switchStatus: '',
      dialogFormVisiblelei: false,
      typeName: '',
      typeStatus: '',
      valuelei: '',
      radio1: 0,
      // 控制弹窗 显示
      dialogVisiblessss: false,
      dialogVisibless: false,
      dialogInfoss: {},
      // 点击查看按钮  这条数据详细信息
      dialogInfo: {},
      listarr: null,
      listLoading: true,
      loading: false,
      templateName: '',
      templateStatus: '',
      templateType: '',
      dialogFormVisible: false,
      del: false,

      ruleForm: {
        templateId: '',
        templateItemName: '',
        spell: '',
        content: '',
        status: '',
        sortNum: ''
      },
      leixFrom: {
        dicTemplateName: '',
        status: '',
        sortNum: ''
      },
      options: [
        {
          id: '1',
          value: '1',
          label: '启用'
        },
        {
          id: '0',
          value: '0',
          label: '禁用'
        }
      ],
      value: '',
      arr: [],
      arrlist: [],
      arrTypelist: [],
      arrTypeAllList: [],
      selectType: '',
      listArr_: [],
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意 : 这里不能为空)
      totalCounts: 0,
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      dialogVisible: false, // 控制弹窗 显示
      operatorCode: "",
      // 点击查看按钮  这条数据详细信息
    }
  },
  created() {
    this.lastTime = new Date().getTime()
    // 类型
  },
  mounted() {
    // 获取当前日期
    const currentDate = new Date();

    // 格式化日期为 YYYY-MM-DD
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
    const day = String(currentDate.getDate()).padStart(2, '0');

    // 设置 orderTime
    this.orderTime = `${year}-${month}-${day}`;
    getUser().then((res) => {
      this.operatorCode = res.data.workNumber;
      this.queryTemplateList(this.PageSize, this.start);
      this.selectTypeList();
      this.selectAllTypeList();
    });
  },
  methods: {
    //模板检索
    queryTemplateList(pageSize, start) {
      getListlei({
        size: pageSize,
        start: start,
        templateItemName: this.templateName,
        templateId: this.templateType == '' ? null : this.templateType,
        status: 1,
        operatorCode: this.operatorCode,
        flag: 1
      }).then((res) => {
        this.listArr_ = res.data
        this.totalCount = res.totalCount
      })
    },
    //所有的类型
    selectAllTypeList() {
      queryList({}).then((res) => {
        this.arrTypeAllList = res.data;
      })
    },
    //所有的类型
    selectTypeList() {
      queryList({
        status: 1
      }).then((res) => {
        //屏蔽自定义出量和自定义引流管的类型
        var itemList = [];
        if (res.data != null && res.data.length > 0) {
          res.data.forEach(item => {
            if (item.dict_template_name != "自定义出量" && item.dict_template_name != "自定义引流管") {
              itemList.push(item);
            }
          });
        }
        this.arrTypelist = itemList;
      })

    },
    //下拉框获取选中的汉字
    selectTppe(id) {
      let selectObj = {};
      selectObj = this.arrTypelist.find((item) => {
        return item.id === id;
      });
      this.selectType = selectObj.dict_template_name;
    },
    // 搜索按钮
    btn() {
      this.start = 1;
      this.queryTemplateList(this.PageSize, this.start)
    },
    // 每页显示的条数
    handleSizeChange(val) {
      this.PageSize = val;
      this.queryTemplateList(this.PageSize, this.start); // 模板
      this.start = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
      this.start = val
      this.queryTemplateList(this.PageSize, this.start) // 模板
    },
    //选择
    handleEdit(info) {
      var item = new Object();
      item.rowIndex = this.rowIndex;
      item.val = info.content;
      eventBus.$emit('selectTemplateResult', item);
    },
    //选择
    handleEdit2(info) {
      var item = new Object();
      item.rowIndex = this.rowIndex;
      item.val = info.orderName;
      eventBus.$emit('selectTemplateResult', item);
    },
    selectPrescription() {
      console.log("病人id：",this.patientId)
      medOrderExecQueryList({
        "drugType": "0",
        "repeatIndicator": "",
        "classType": "0,8",
        "orderStatus": "",
        "isSync": 5,
        "size": this.PageSize2,
        "start": this.start2,
        "orderTime": this.orderTime,
        "patientId": this.patientId,
      }).then((res) => {
        this.lists = res.data;
        this.totalCount2 = res.totalCount;
      });
      this.noPrescriptionDialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.template-select-diaolog {
  .input {
    ::v-deep .el-input__inner {
      padding: 0 5px;
    }
  }
}
</style>
